package com.recruit.wechat.controller;


import com.recruit.wechat.controller.helper.$;
import com.recruit.wechat.controller.model.ApiResultModel;
import com.recruit.wechat.utils.FileUtilsDelete;
import com.recruit.wechat.utils.FtpToPDFUtil;
import com.recruit.wechat.utils.FtpUtil;
import com.recruit.wechat.utils.WordToPDF;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.recruit.wechat.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.recruit.wechat.bean.WechatEnclosure;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LF
 * @since 2020-08-25
 */
@Controller
@RequestMapping("/WeChatUser/wechatEnclosure")
public class WechatEnclosureController extends BaseController{
     /** 主页面 */
    @RequestMapping("admin/main.html")
    public String main(){
       return  "wechatEnclosure/WechatenclosureMain";
    }
    /** 添加页面跳转
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addWechatEnclosureHtml")
    public String addWechatEnclosureHtml( ){
             return "wechatEnclosure/WechatenclosureAdd";
    }
    /** 编辑页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editWechatEnclosureHtml")
    public String editWechatEnclosureHtml(Integer id ,Model model ){
           model.addAttribute("wechatEnclosure",wechatEnclosureService.selectWechatEnclosureByIdWechatEnclosure(id));
             return "wechatEnclosure/WechatenclosureEdit";
    }

    /** 查看页面跳转
     * @param  id  前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showWechatEnclosureHtml")
    public String showWechatEnclosureHtml(Integer id ,Model model ){
           model.addAttribute("wechatEnclosure",wechatEnclosureService.selectWechatEnclosureByIdWechatEnclosure(id));
             return "wechatEnclosure/WechatenclosureShow";
    }


    /** 分页查询WechatEnclosure
     * @param page  页码
     * @param limit  查几条
     * @param wechatEnclosure  前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findWechatEnclosureByPage")
    @ResponseBody
    public ResultPage findWechatEnclosureByPage(Integer page, Integer limit,WechatEnclosure wechatEnclosure){
             return wechatEnclosureService.findWechatEnclosureByPage(page,limit,wechatEnclosure);
    }

    /** 添加WechatEnclosure
     * @param wechatEnclosure  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/addWechatEnclosure")
    @ResponseBody
    public ApiResultModel saveWechatEnclosure(WechatEnclosure wechatEnclosure  ){
             return wechatEnclosureService.addWechatEnclosure(wechatEnclosure);
    }

    /** 编辑WechatEnclosure
     * @param wechatEnclosure  前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editWechatEnclosure")
    @ResponseBody
    public ApiResultModel editWechatEnclosure(WechatEnclosure wechatEnclosure  ){
             return wechatEnclosureService.editWechatEnclosure(wechatEnclosure);
    }



    /** 删除一条WechatEnclosure
     * @param id  前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delWechatEnclosure")
    @ResponseBody
    public ApiResultModel delWechatEnclosure( Integer id  ){
             return wechatEnclosureService.delWechatEnclosure(id);
    }


   /* {
        "code": 200
            ,"msg": ""
            ,"data": {
                "src": "http://cdn.layui.com/123.jpg"
            }
    }*/
    @PostMapping("admin/upload")
    @ResponseBody
    public ApiResultModel upload(@RequestParam("file") MultipartFile file) throws Exception {
        /**
        //创建暂存文件路径
        String oldfile= "C:\\CCC\\";
        //文件转换
        String pdfurl = WordToPDF.toPDF(file,oldfile);
        //System.out.println(pdfurl+"========pdfurl");
        //创建file文件对象
        File pdfFile = new File(pdfurl);
        //创建文件输入流
        FileInputStream fileInputStream = new FileInputStream(pdfFile);
        //创建MultipartFile对象
        MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
        //上传PDF
        String finaName = FtpUtil.uploadFile(multipartFile);
        //返回服务器文件全路径
        String nameURL = FtpUtil.ReturnUrl(finaName);
        System.out.println("服务器文件保存路径====="+nameURL);

        //上传完成后删除本地暂存文件
        boolean flag = FileUtilsDelete.delete(oldfile);

        if (flag){
            return $.success(200,"上传成功",nameURL);
        }else {
            return $.error(0,"上传失败");
        }

         **/

        //判断上传文件类型是否是.doc .docx .pdf
        Boolean aBoolean = FtpToPDFUtil.ValidFileType(file);
        //如果返回是false则提示不符合上传文件类型
        if (aBoolean==false){
            return $.error(400,"文件类型不符合,请重新上传！");
        }

        String PDFurl= FtpToPDFUtil.uploadFile(file);
        if (PDFurl.length()<1){
            return $.error(0,"上传失败");
        }else {
            return $.success(200,"上传成功",PDFurl);
        }

    }


}
